package com.jcfk.eam.service.mould;

import com.jcfk.eam.domain.po.main.EamAssetInfo;
import com.jcfk.eam.domain.po.mould.EamMouldTaskInfo;
import com.jcfk.eam.domain.po.mould.EamMouldWorkRecord;
import com.jcfk.eam.domain.query.mould.EamMouldWorkRecordQuery;
import com.jcfk.eam.domain.dto.mould.EamMouldWorkRecordDTO;
import com.jcfk.eam.dao.mould.EamMouldWorkRecordDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.service.main.EamAssetInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import com.jcfk.common.core.utils.IdUtils;
import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 模具操作记录表
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2025-07-30
 */
@Service
public class EamMouldWorkRecordService extends BaseService<EamMouldWorkRecordDao,EamMouldWorkRecord> {

    @Autowired
    private EamAssetInfoService eamAssetInfoService;

    /**
     * EamMouldWorkRecord分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamMouldWorkRecordDTO> queryByPage(DataPage<EamMouldWorkRecordDTO> page, EamMouldWorkRecordQuery query) {
        // 分页查询
        query.setMouldCode(StringUtils.isEmpty(query.getMouldCode()) ? query.getMouldCode() : query.getMouldCode() + "%");
        query.setMouldName(StringUtils.isEmpty(query.getMouldName()) ? query.getMouldName() : query.getMouldName() + "%");
        query.setMouldType(StringUtils.isEmpty(query.getMouldType()) ? query.getMouldType() : query.getMouldType() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


        /**
     * 判断EamMouldWorkRecord是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamMouldWorkRecordDTO dto) {
        QueryWrapper<EamMouldWorkRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
                                                                                    
        return RetResult.ok();
    }

    /**
     * 插入模具操作记录表
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamMouldWorkRecordDTO dto) {

        // 保存
        EamMouldWorkRecord item = new EamMouldWorkRecord();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新模具操作记录表
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamMouldWorkRecordDTO dto) {

        // 保存
        EamMouldWorkRecord item = new EamMouldWorkRecord();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除模具操作记录表(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamMouldWorkRecordDTO dto) {

        EamMouldWorkRecord item = new EamMouldWorkRecord();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamMouldWorkRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamMouldWorkRecord item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
                item.setDeleted(0);
                                                                                                                                                                                                                item.setCreatedUserName(SecurityUtils.getUserName());
                                                                                }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

                                                                                    item.setLastUpdatedUserName(SecurityUtils.getUserName());
            
    }

    /**
     * 根据主键获取模具操作记录表信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamMouldWorkRecordDTO get(String id) {
        // 查询数据
        EamMouldWorkRecord item = this.baseMapper.selectById(id);
            EamMouldWorkRecordDTO dto = new EamMouldWorkRecordDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMouldWorkRecordDTO>
     */
    public List<EamMouldWorkRecordDTO> getList(EamMouldWorkRecordQuery query) {
        // 查询条件
        QueryWrapper<EamMouldWorkRecord> qw = new QueryWrapper<>();
                                                                                                                                                                        qw.lambda().likeRight(!StrUtil.isEmpty(query.getMouldCode()), EamMouldWorkRecord::getMouldCode, query.getMouldCode());
                                                    qw.lambda().likeRight(!StrUtil.isEmpty(query.getMouldName()), EamMouldWorkRecord::getMouldName, query.getMouldName());
                                                    qw.lambda().likeRight(!StrUtil.isEmpty(query.getMouldType()), EamMouldWorkRecord::getMouldType, query.getMouldType());
                                                                                                                    qw.lambda().eq(query.getWorkType()!=null, EamMouldWorkRecord::getWorkType, query.getWorkType());
                                                                                    qw.lambda().ge(!StrUtil.isEmpty(query.getWorkTimeStart()), EamMouldWorkRecord::getWorkTime, query.getWorkTimeStart());
        qw.lambda().le(!StrUtil.isEmpty(query.getWorkTimeEnd()), EamMouldWorkRecord::getWorkTime, query.getWorkTimeEnd());
                                                                                                                                                                                                                                                                                                                            // 查询数据
        List<EamMouldWorkRecord> list = super.list(qw);

        return convertList(list, EamMouldWorkRecordDTO.class);
    }

    /**
     * 根据主键获取模具操作记录表信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
            EamMouldWorkRecord item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
        }

    /**
     * 批量保存
     * @param mouldCodes
     * @param status
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<String> mouldCodes, Integer status) {

        List<EamMouldWorkRecord> savelist = new ArrayList<>();
        QueryWrapper<EamAssetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(EamAssetInfo::getAssetCode, mouldCodes);
        queryWrapper.lambda().eq(EamAssetInfo::getDeleted, 0);
        List<EamAssetInfo> findList = eamAssetInfoService.list(queryWrapper);
        findList.forEach(itemOne -> {
            // 保存
            EamMouldWorkRecord item = new EamMouldWorkRecord();
            this.setDefaultValue(item);
            item.setMouldCode(itemOne.getAssetCode());
            item.setMouldName(itemOne.getAssetName());
            item.setLocationName(itemOne.getLocationName());
            item.setLocationId(itemOne.getLocationId());
            item.setMouldType(itemOne.getAssetTypeName());
            item.setWorkTime(new Date());
            item.setWorkUser(SecurityUtils.getUserName());
            item.setWorkType(status);
            savelist.add(item);
        });
        // 保存
        if (savelist.size() > 0) super.saveBatch(savelist);
    }

    /**
     * 批量保存
     * @param mouldCodes
     * @param status
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<String> mouldCodes, Integer status, EamMouldTaskInfo eamMouldTaskInfo) {

        List<EamMouldWorkRecord> savelist = new ArrayList<>();
        QueryWrapper<EamAssetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(EamAssetInfo::getAssetCode, mouldCodes);
        queryWrapper.lambda().eq(EamAssetInfo::getDeleted, 0);
        List<EamAssetInfo> findList = eamAssetInfoService.list(queryWrapper);
        findList.forEach(itemOne -> {
            // 保存
            EamMouldWorkRecord item = new EamMouldWorkRecord();
            this.setDefaultValue(item);
            item.setMouldCode(itemOne.getAssetCode());
            item.setMouldName(itemOne.getAssetName());
            item.setLocationName(itemOne.getLocationName());
            item.setLocationId(itemOne.getLocationId());
            item.setMaterialCode(eamMouldTaskInfo.getMaterialCode());
            item.setMaterialName(eamMouldTaskInfo.getMaterialName());
            item.setMouldType(itemOne.getAssetTypeName());
            item.setWorkTime(new Date());
            item.setWorkUser(SecurityUtils.getUserName());
            item.setWorkType(status);
            savelist.add(item);
        });
        // 保存
        if (savelist.size() > 0) super.saveBatch(savelist);
    }
}

